package com.mujin.keji.collectionsystem.db.dao;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;

import com.mujin.keji.collectionsystem.db.domain.SaveVisitRecordBean;
import com.mujin.keji.collectionsystem.db.domain.VisitApplyBean;
import com.mujin.keji.collectionsystem.db.domain.VisitApplyDetailBean;
import com.mujin.keji.collectionsystem.db.domain.VisitApplyMarkBean;
import com.mujin.keji.collectionsystem.db.domain.VisitCountBean;

import java.util.List;

import io.reactivex.Flowable;

/**
 * 外访守则操作
 */
@Dao
public interface VisitDao {

    /**
     * 查询外访条数，待外访，已外访，已完成
     * @return
     */
    @Query("select * from rc_cp_visit_count where userName = (:userName)")
    Flowable<List<VisitCountBean>> findVisitCount(String userName);

    @Insert
    void addVisitCount(VisitCountBean ...visitCountBeans);

    @Insert
    void addVisitList(VisitApplyBean ...visitApplyBeans);

    @Query("select * from rc_cp_visit_list where status = (:status) and userName = (:userName)")
    Flowable<List<VisitApplyBean>> queryVisitListByStatus(String status,String userName);

    @Query("select * from rc_cp_visit_list where status = (:status) and userName = (:userName) order by visitAddress  ASC")
    Flowable<List<VisitApplyBean>> queryVisitListByStatusOrderByAdressASC(String status,String userName);


    @Query("select * from rc_cp_visit_list where status = (:status) and userName = (:userName) order by visitAddress  DESC")
    Flowable<List<VisitApplyBean>> queryVisitListByStatusOrderByAdressDESC(String status,String userName);


    @Query("select * from rc_cp_visit_list where status = (:status) and userName = (:userName) order by visitName ASC")
    Flowable<List<VisitApplyBean>> queryVisitListByStatusOrderByAdressNameASC(String status,String userName);


    @Query("select * from rc_cp_visit_list where status = (:status) and userName = (:userName) order by visitName DESC")
    Flowable<List<VisitApplyBean>> queryVisitListByStatusOrderByAdressNameDESC(String status,String userName);

    /**
     * 根据外访任务id查询外访记录
     * @param applyId
     * @return
     */
    @Query("select * from rc_cp_visit_list where visitApplyId = (:applyId)")
    VisitApplyBean queryVisitList(Long applyId);

    /**
     * 更新案件列表
     * @param visitApplyBeans
     */
    @Update
    void updateVisitApply(VisitApplyBean ...visitApplyBeans);

    /**
     * 根据案件编号查询案件详情
     * @param caseCode
     * @return
     */
    @Query("select * from rc_cp_visit_detail where caseCode = (:caseCode)")
    VisitApplyDetailBean queryVisitApplyDetailByCaseCode(String caseCode);

    //新增案件详情
    @Insert
    void addVisitApplyDetail(VisitApplyDetailBean ... visitApplyDetailBeans);

    //修改案件详情
    @Update
    void updateVisitApplyDetail(VisitApplyDetailBean ... visitApplyDetailBeans);
    /**
     * 根据案件id查询案件催记
     * @param caseId
     * @return
     */
    @Query("select * from rc_cp_visit_apply_mark where caseId = (:caseId)")
    VisitApplyMarkBean queryVisitApplyMarkByCaseId(String caseId);

    //新增案件催记详情
    @Insert
    void addVisitApplyMark(VisitApplyMarkBean ... visitApplyMarkBeans);

    //修改案件催记详情
    @Update
    void updateVisitApplyMark(VisitApplyMarkBean ... visitApplyMarkBeans);


    /**
     * 移动方查询案件详情
     * @param caseCode
     * @return
     */
    @Query("select * from rc_cp_visit_detail where caseCode = (:caseCode)")
    Flowable<VisitApplyDetailBean> queryVisitDetail(String caseCode);

    /**
     * 移动方查询案件催记
     * @param caseId
     * @return
     */
    @Query("select * from rc_cp_visit_apply_mark where caseId = (:caseId)")
    Flowable<VisitApplyMarkBean> queryVisitMark(String caseId);



    /**
     * 查询外访条数，待外访，已外访，已完成
     * @return
     */
    @Query("select * from rc_cp_visit_count")
    VisitCountBean findVisitCountDetail();

    @Update
    void updateVisitCount(VisitCountBean ...visitCountBeans);


    @Query("select * from rc_cp_visitor_record where visitApplyId = (:applyId)")
    Flowable<SaveVisitRecordBean> queryAllVisitRecord(Long applyId);


    @Query("delete from rc_cp_visit_list where status = (:status)")
    void deleteVisitListByUserName(String status);

    @Query("select * from rc_cp_visit_list where status = (:status) and userName = (:userName)")
    List<VisitApplyBean> queryVisitByStatus(String status,String userName);


    @Query("SELECT * FROM rc_cp_visitor_record WHERE visitApplyId IN (:applyIds)")
    Flowable<List<SaveVisitRecordBean>> queryAllVisitRecordList(List<Long> applyIds);



}
